﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using KaoQin.Model;
using System.Drawing;

namespace KaoQin.Common
{
    public class FileHelper
    {
        /// <summary>
        /// 获得指定目录下的所有文件名称
        /// </summary>
        /// <param name="dirPath">目录路径</param>
        /// <returns>文件名列表</returns>
        public static List<string> GetFileList(string dirPath)
        {
            List<string> result = null;

            if (Directory.Exists(dirPath))
            {
                result = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories).ToList<string>();
            }

            return result;
        }


        /// <summary>
        /// 获得指定目录下指定文件类型的文件名称
        /// </summary>
        /// <param name="dirPath">目录路径</param>
        /// <param name="fileType">文件类型</param>
        /// <returns>文件名列表</returns>
        public static List<string> GetFileList(string dirPath, string fileType)
        {
            List<string> result = null;

            if (Directory.Exists(dirPath))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dirPath);

                result = (from f in dirInfo.GetFiles("*." + fileType, SearchOption.AllDirectories)
                          select f.Name).ToList<string>();

            }

            return result;
        }

        /// <summary>
        /// 修改文件名称
        /// </summary>
        /// <param name="dirPath"></param>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public static bool ReName(string dirPath, string oldName, string newName)
        {
            bool result = false;
            string filePath = dirPath + "\\" + oldName;
            if (!File.Exists(filePath))
            {
                return result;
            }
            else
            {
                try
                {
                    File.Move(filePath, dirPath + "\\" + newName);
                    result = true;
                }
                catch (IOException)
                {

                }
            }

            return result;
        }

        /// <summary>
        /// 复制一个文件，以新名字命名
        /// </summary>
        /// <param name="srcFileName"></param>
        /// <param name="desDir"></param>
        /// <param name="desFileName"></param>
        /// <returns></returns>
        public static bool CopyFile(string srcFileName, string desDir, string desFileName)
        {
            bool result = false;

            if (!File.Exists(srcFileName))
            {
                return result;
            }
            string filePaht = desDir + "\\" + desFileName;
            if (CreateDirectory(desDir))
            {
                try
                {
                    File.Copy(srcFileName, filePaht);
                    result = true;
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }

            return result;

        }

        /// <summary>
        /// 删除一个文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool DeleteFile(string filePath)
        {
            bool result = false;

            try
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                    result = true;
                }
            }
            catch (IOException)
            { }

            return result;
        }

        /// <summary>
        /// 将二进制流保存到文件
        /// </summary>
        /// <param name="data">字节流</param>
        /// <param name="path">文件路径</param>
        /// <param name="fileName">文件名称（包含扩展名）</param>
        /// <returns></returns>
        public static bool SaveFile(byte[] data, string path, string fileName)
        {
            bool result = false;

            string filePaht = path + "\\" + fileName;

            try
            {
                FileStream fs = new FileStream(filePaht, FileMode.OpenOrCreate);

                BinaryWriter writer = new BinaryWriter(fs);

                writer.Write(data);

                writer.Close();
                fs.Close();
                writer.Dispose();
                fs.Dispose();


                result = true;
            }
            catch (IOException ex)
            {
                throw ex;
            }


            return result;

        }

        

        /// <summary>
        /// 将文件读成字节流
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static byte[] LoadFile(string filePath)
        {
            byte[] result = null;

            try
            {
                FileStream stream = System.IO.File.OpenRead(filePath);
                result = new byte[stream.Length];

                stream.Read(result, 0, result.Length);
                stream.Close();
                stream.Dispose();
            }
            catch (IOException ex)
            {

                throw ex;
            }

            return result;
        }




        /// <summary>
        /// 得到所有磁盘驱动器
        /// </summary>
        /// <returns></returns>
        public static string[] AllDrivers()
        {
            return Directory.GetLogicalDrives();
        }

        /// <summary>
        /// 获得当前目录
        /// </summary>
        /// <returns></returns>
        public static string GetcurrPath()
        {
            return Application.StartupPath;
        }

        /// <summary>
        /// 创建目录(不存在则创建)
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool CreateDirectory(string path)
        {
            bool result = false;

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                result = true;
            }
            catch (IOException)
            {

            }

            return result;
        }

        




    }
}
